package pub.guoxin.oops.manager.service.impl;

import com.querydsl.core.types.dsl.BooleanExpression;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import pub.guoxin.oops.manager.service.GroupService;
import pub.guoxin.oops.manager.vo.GroupIdNameVo;
import pub.guoxin.oops.manager.vo.GroupPageVo;
import pub.guoxin.oops.manager.vo.GroupVo;
import pub.guoxin.oops.model.Group;
import pub.guoxin.oops.model.QGroup;
import pub.guoxin.oops.model.RealTimeCommunication;
import pub.guoxin.oops.model.User;
import pub.guoxin.oops.repository.GroupRepository;
import pub.guoxin.oops.repository.RealTimeCommunicationRepository;
import pub.guoxin.oops.repository.UserRepository;

import java.util.List;
import java.util.Objects;

/**
 * Created by guoxin on 18-1-30.
 */
@Service
public class GroupServiceImpl implements GroupService {

    QGroup q = QGroup.group;
    @Autowired
    private GroupRepository groupRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private RealTimeCommunicationRepository realTimeCommunicationRepository;

    @Override
    public Group add(User user, GroupVo groupAddVo) {
        Group group = groupAddVo.create();
        User one = userRepository.findOne(group.getUserId());
        if (Objects.isNull(one)) {
            return null;
        }
        group.init();
        group.setCompanyId(user.getCompanyId());
        return groupRepository.save(group);
    }

    @Override
    public Group del(String id) {
        Group one = groupRepository.findOne(id);
        if (Objects.isNull(one)) {
            return null;
        }

        // 去掉通讯模块关系
        List<RealTimeCommunication> communications = realTimeCommunicationRepository.findByGroupId(one.getId());
        communications.forEach(communication -> communication.setGroupId(null));
        realTimeCommunicationRepository.save(communications);

        one.setUsed(false);

        return groupRepository.save(one);
    }

    @Override
    public Page<GroupPageVo> page(User user, Integer pageNumber, Integer pageSize) {
        BooleanExpression expression = q.used.isTrue();
        expression = expression.and(q.companyId.eq(user.getCompanyId()));
        Pageable request = new PageRequest(pageNumber - 1, pageSize, Sort.Direction.DESC, "creation");
        Page<Group> all = groupRepository.findAll(expression, request);
        Page<GroupPageVo> page = all.map(group -> {
            User one = userRepository.findOne(group.getUserId());
            return new GroupPageVo(group.getId(), group.getName(), group.getCreation(), one);
        });
        return page;
    }

    @Override
    public Group modify(User user, GroupVo groupVo) {
        Group group = groupRepository.findOne(groupVo.getId());
        if (Objects.isNull(group)) {
            return null;
        }
        group.setName(groupVo.getName());
        group.setUserId(groupVo.getUserId());
        return groupRepository.save(group);
    }

    @Override
    public List<GroupIdNameVo> list(String companyId) {
        List<Group> groups = groupRepository.findByCompanyIdAndUsed(companyId, true);
        List<GroupIdNameVo> result = Lists.newArrayList();
        groups.stream().map(GroupIdNameVo::new).forEach(result::add);
        return result;
    }
}
